#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include "file-list.h"
#include "dir.h"



int read_file_list(char * path, char * plist, unsigned int * count)
{
  DIR * dir;
  struct dirent * ptr;
  char base[1000];
  struct stat st;

  if ((dir = opendir(path)) == NULL) {
    perror("open dir error...");
    exit(1);
  }

  while ((ptr = readdir(dir)) != NULL) {
    if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
      continue;
    else if (ptr->d_type == 8) 	/* file */
      printf("file:%s/%s\n", path, ptr->d_name);
    else if (ptr->d_type == 10)	/* link file */
      printf("link:%s/%s\n", path, ptr->d_name);
    else if (ptr->d_type == 4) {/* dir */
      memset(base, 0, sizeof base);
      strcpy(base, path);
      strcat(base, "/");
      strcat(base, ptr->d_name);
      //read_file_list(base);
    }
	
	*count++;

    stat(ptr->d_name, &st);
    printf("size = %f(kb) %s %s %s\n", (float)st.st_size/1000.0, 
		ctime(&st.st_ctime),  /* created  */
		ctime(&st.st_atime),  /* access   */
		ctime(&st.st_mtime)); /* modified */
  }
  
  closedir(dir);
  
  return 1;
}

int get_file_size(char * filename)
{
  struct stat temp;

  stat(filename, &temp);

  return temp.st_size;
}

void analysis_file_time(char * filename)
{
  char time[20] = {0};
  unsigned char i;

  strncpy(time, filename, 4);
  time[4] = '-';
  strncpy(time+5, filename+4, 2);
  time[7] = '-';
  strncpy(time+8, filename+6, 2);
  time[10] = ' ';
  strncpy(time+11, filename+9, 2);
  time[13] = ':';
  strncpy(time+14, filename+11, 2);
  time[16] = ':';
  strncpy(time+17, filename+13, 2);

  printf("time = %s\n", time);
}

int main(void)
{
  DIR * dir;
  //char path[1000];
  char * path = "/home/share/work/test-verification/dir";
  int size = 0;
  char * filename = "20180920_200416_video.h264";	// 2018-09-20 20:04:16

  file_list * f_head, * f_newnode, *f_posnode;

	NEW_LIST_NODE(file_list, f_head);
	INIT_LIST_HEAD(&f_head->list);
  
  //read_file_list(path);
  analysis_file_time(filename);


}
